package com.sean.item;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.sean.activity.R;
import com.sean.util.UnitUtil;

public class SelectorView extends ViewGroup {
	
	private int mPadding;

	public SelectorView(Context context) {
		super(context);
		this.setBackgroundResource(R.drawable.selector_bg);
		mPadding = (int)UnitUtil.dip2px(context, 10);
	}

	/**
	 * 计算每个子控件的位置
	 */
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		final int childCount = getChildCount();
		int left = mPadding;
		int top = mPadding;
		View child = null;
		for(int i = 0; i < childCount; i++) {
			child = getChildAt(i);
			if(child.getVisibility() == View.GONE) 
				continue;
			child.layout(left, top, left + child.getMeasuredWidth(), top + child.getMeasuredHeight());
			left += child.getMeasuredWidth();
		}
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		final int childCount = getChildCount();
		int width = (this.getMeasuredWidth() - mPadding - mPadding) / childCount;
		int height = this.getMeasuredHeight() - mPadding - mPadding;
		int widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
		int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
		this.measureChildren(widthSpec, heightSpec);
		
	}

}
